package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.PaymentCallbacks;
import com.example.mall.dto.PaymentCallbacksDto;
import com.example.mall.mapper.PaymentCallbacksMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PaymentCallbacksQuery;
import com.example.mall.service.IPaymentCallbacksService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付回调记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class PaymentCallbacksServiceImpl extends ServiceImpl<PaymentCallbacksMapper, PaymentCallbacks> implements IPaymentCallbacksService {

    @Autowired
    private PaymentCallbacksMapper paymentCallbacksMapper;

    @Override
    public PageDTO<PaymentCallbacksDto> queryPage(PaymentCallbacksQuery paymentCallbacksQuery) {
        if (paymentCallbacksQuery == null) {
            paymentCallbacksQuery = new PaymentCallbacksQuery();
        }
        Page<PaymentCallbacks> page = paymentCallbacksQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PaymentCallbacks> cartPage = lambdaQuery()
                .like(paymentCallbacksQuery.getCallbackId()!= null, PaymentCallbacks::getCallbackId, paymentCallbacksQuery.getCallbackId())
                .like(paymentCallbacksQuery.getPaymentId()!= null, PaymentCallbacks::getPaymentId, paymentCallbacksQuery.getPaymentId())
                .like(paymentCallbacksQuery.getCallbackStatus()!= null, PaymentCallbacks::getCallbackStatus, paymentCallbacksQuery.getCallbackStatus())
                .like(paymentCallbacksQuery.getCallbackTime()!= null, PaymentCallbacks::getCallbackTime, paymentCallbacksQuery.getCallbackTime())
                .like(paymentCallbacksQuery.getOrderId()!= null, PaymentCallbacks::getOrderId, paymentCallbacksQuery.getOrderId())
                .orderByDesc(PaymentCallbacks::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PaymentCallbacksDto.class);
    }

    @Override
    public PaymentCallbacks queryPaymentCallbacksById(Long callbackId) {
        AssertUtil.isNotNull(callbackId, "callbackId不能为空");
        return paymentCallbacksMapper.selectById(callbackId);
    }

    @Override
    public List<PaymentCallbacks> queryPaymentCallbacksList() {
        return this.list();
    }

    @Override
    public void insert(PaymentCallbacksDto paymentCallbacksDto) {
        PaymentCallbacks paymentCallbacks = new PaymentCallbacks();
        CopyUtils.copyProperties(paymentCallbacksDto, paymentCallbacks);
        paymentCallbacks.setCallbackId(CodeGenerateUtils.generateUniqueId());
        paymentCallbacks.setCreateDate(new Date());
        paymentCallbacks.setUpdateDate(new Date());
        paymentCallbacks.setCallbackStatus(BaseConstants.NOT_USED);
        paymentCallbacksMapper.insert(paymentCallbacks);
    }

    @Override
    public void update(PaymentCallbacksDto paymentCallbacksDto) {
        AssertUtil.isNotNull(paymentCallbacksDto.getCallbackId(), "callbackId不能为空");
        PaymentCallbacks paymentCallbacks = paymentCallbacksMapper.selectById(paymentCallbacksDto.getCallbackId());
        AssertUtil.isNotNull(paymentCallbacks, "支付回调记录不存在");
        CopyUtils.copyProperties(paymentCallbacksDto, paymentCallbacks);
        paymentCallbacks.setUpdateDate(new Date());
        paymentCallbacksMapper.updateById(paymentCallbacks);
    }

    @Override
    public void delete(Long callbackId) {
        AssertUtil.isNotNull(callbackId, "callbackId不能为空");
        PaymentCallbacks paymentCallbacks = paymentCallbacksMapper.selectById(callbackId);
        AssertUtil.isNotNull(paymentCallbacks, "支付回调记录不存在");
        paymentCallbacks.setCallbackStatus(BaseConstants.CANCEL);
        paymentCallbacks.setUpdateDate(new Date());
        paymentCallbacksMapper.updateById(paymentCallbacks);
    }

    @Override
    public void deleteBatch(List<Long> callbackIds) {
        AssertUtil.isNotNull(callbackIds, "callbackIds不能为空");
        List<PaymentCallbacks> paymentCallbacks = paymentCallbacksMapper.selectBatchIds(callbackIds);
        AssertUtil.isNotNull(paymentCallbacks, "支付回调记录不存在");
        for (PaymentCallbacks paymentCallback : paymentCallbacks) {
            paymentCallback.setCallbackStatus(BaseConstants.CANCEL);
            paymentCallback.setUpdateDate(new Date());
            paymentCallbacksMapper.updateById(paymentCallback);
        }
    }
}
